package com.thinkfit.jy.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.common.lib.activity.BaseActivity;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.manager.DataManager;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.common.lib.utils.BaseUtils;
import com.common.lib.utils.LogUtil;
import com.common.lib.view.banner.BannerView;
import com.common.lib.view.banner.HolderCreator;
import com.thinkfit.jy.R;
import com.thinkfit.jy.adapter.BannerViewHolder;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.SportsStatusBarViewUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Random;

/**
 * 屏保界面
 */
public class ScreenProtectActivity extends BaseActivity<EmptyContract.Presenter> implements EmptyContract.View {

    private PowerManager.WakeLock mWakeLock;
    private int mCurrentStyle;
    private Random mRandom;

    private int mXDirection, mYDirection;
    private int mScreenWidth, mScreenHeight;
    private long mCount;

    @Override
    protected int getLayoutId() {
        mCurrentStyle = DataManager.Companion.getInstance().getScreenProtectStyle();
        return getResources().getIdentifier("activity_screen_protect_style" + mCurrentStyle,
                "layout", getPackageName());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rootView:
                finish();
                break;
        }
    }

    @SuppressLint("InvalidWakeLockTag")
    @Override
    protected void onCreated(@Nullable Bundle savedInstanceState) {
        setViewsOnClickListener(R.id.rootView);
        if (DataManager.Companion.getInstance().getMainStyle() == 3) {
            setBackground(R.id.rootView, R.drawable.app_bg_xm);
        }
        mRandom = new Random();
        PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.SCREEN_DIM_WAKE_LOCK |
                PowerManager.ON_AFTER_RELEASE, "SimpleTimer");
        switch (mCurrentStyle) {
            case 1:
            case 2:
                setTextTypeFace(R.id.tvCurrentTime, R.id.tvDate);
                resetDate();
                break;
            case 3:
                mXDirection = 0;
                mYDirection = 0;
                mScreenWidth = getDisplayMetrics().widthPixels;
                mScreenHeight = getDisplayMetrics().heightPixels;
                mCount = 0;
                break;
            case 5:
                showBannerView();
                break;
        }
    }

    private void showBannerView() {
        BannerView bannerView = findViewById(R.id.bannerView);
        bannerView.setIndicatorVisible(true);
        bannerView.setPages(DataManager.Companion.getInstance().getBannerList(),
                new HolderCreator<BannerViewHolder>() {
                    @Override
                    public BannerViewHolder createViewHolder() {
                        return new BannerViewHolder();
                    }
                });
        bannerView.start();
    }

    @Override
    protected void onResume() {
        mWakeLock.acquire();
        super.onResume();
        SportsStatusBarViewUtils.getInstance().goneLayout();
    }

    @Override
    protected void onPause() {
        mWakeLock.release();
        super.onPause();


    }

    @Override
    protected void onStop() {
        super.onStop();
        System.out.println("ddddddddddddddddddddddd   ScreenProtectActivity ");
//        DeviceTypeUtils.showBackButton(getApplicationContext());
//        SportsStatusBarViewUtils.getInstance().initView(getContext().getApplicationContext());

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtil.LogE("onDestroy");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceive(HashMap<String, Object> map) {
        if (map == null || isFinish()) {
            return;
        }
        if (map.containsKey(EventBusEvent.TIME_CHANGE)) {
            if (mCurrentStyle == 1 || mCurrentStyle == 2) {
                resetDate();
            } else if (mCurrentStyle == 3) {
                moveLogo();
            }
        } else {
            super.onReceive(map);
        }
    }

    private void moveLogo() {
        if (mCount++ % 20 != 0) {
            return;
        }
        ImageView ivLogo = findViewById(R.id.ivLogo);
        float newX, newY;
        int moveX = Math.abs(mRandom.nextInt() % 10);
        if (mXDirection == 0) {
            newX = ivLogo.getX() - moveX;
        } else {
            newX = ivLogo.getX() + moveX;
        }
        int moveY = Math.abs(mRandom.nextInt() % 5);
        if (mYDirection == 0) {
            newY = ivLogo.getY() - moveY;
        } else {
            newY = ivLogo.getY() + moveY;
        }
        if (newX < 0) {
            newX = 0;
            mXDirection = 1;
        } else if (newX > mScreenWidth - ivLogo.getWidth()) {
            newX = mScreenWidth - ivLogo.getWidth();
            mXDirection = 0;
        }
        if (newY <= 0) {
            newY = 0;
            mYDirection = 1;
        } else if (newY > mScreenHeight) {
            newY = mScreenHeight;
            mYDirection = 0;
        }
        ivLogo.setX(newX);
        ivLogo.setY(newY);
    }

    private void resetDate() {
        Calendar now = Calendar.getInstance();
        if (now.get(Calendar.AM_PM) == Calendar.AM) {
            setText(R.id.tvCurrentTime, BaseUtils.StaticParams.getNewText(now.get(Calendar.HOUR_OF_DAY))
                    + ":" + BaseUtils.StaticParams.getNewText(now.get(Calendar.MINUTE)) + " " + getString(R.string.app_am));
        } else {
            setText(R.id.tvCurrentTime, BaseUtils.StaticParams.getNewText(now.get(Calendar.HOUR_OF_DAY) - 12)
                    + ":" + BaseUtils.StaticParams.getNewText(now.get(Calendar.MINUTE)) + " " + getString(R.string.app_pm));
        }
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH) + 1;
        int day = now.get(Calendar.DAY_OF_MONTH);
        int weekDay = now.get(Calendar.DAY_OF_WEEK);
        setText(R.id.tvDate, year + "/" + BaseUtils.StaticParams.getNewText(month) + "/" + BaseUtils.StaticParams.getNewText(day) + " " + getString(getResources().getIdentifier("app_weekday_" + weekDay, "string", getPackageName())));
    }

    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }
}